package com.salary.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.util.GetMonth;
import com.common.util.GetYear;
import com.salary.dao.SalaryRecordRepository;
import com.salary.entity.BasicSalary;
import com.salary.entity.SalaryRecord;
import com.salary.entity.dto.SalaryRecordDTO;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;

@Service
@Transactional
public class SalaryRecordService implements ISalaryRecordService {

	@Autowired
	private SalaryRecordRepository salaryRecordDao;
	@Autowired
	private StaffRepository staffDao;
	
	public void saveOrUpdate(SalaryRecord salaryRecord) {
		salaryRecordDao.save(salaryRecord);
	}

	public void delete(SalaryRecord salaryRecord) {
		salaryRecordDao.delete(salaryRecord);
	}

	public void delete(List<Long> ids) {
		for(Long id:ids) {
			salaryRecordDao.delete(salaryRecordDao.findOne(id));
		}
	}

	public SalaryRecord findOne(Long id) {
		return salaryRecordDao.findOne(id);
	}

	public List<SalaryRecord> findAll() {
		return (List<SalaryRecord>) salaryRecordDao.findAll();
	}

	public Page<SalaryRecord> findAll(Specification<SalaryRecord> spec, Pageable pageable) {
		return salaryRecordDao.findAll(spec, pageable);
	}

	public Staff findStaff(SalaryRecord salaryRecord) {
		SalaryRecord sr = salaryRecordDao.findOne(salaryRecord.getId());
		return sr.getStaff();
	}

	public BasicSalary findBasicSalary(SalaryRecord salaryRecord) {
		SalaryRecord sr = salaryRecordDao.findOne(salaryRecord.getId());
		return sr.getBasicSalary();
	}

	public List<Integer> getYears() {
		List<Integer> years = new ArrayList<Integer>();
		List<SalaryRecord> list = (List<SalaryRecord>) salaryRecordDao.findAll();
		for(SalaryRecord sr:list) {
			if(sr.getAccountTime()!=null) {
				years.add(GetYear.getYear(sr.getAccountTime()));
			}
		}
		HashSet h = new HashSet(years);
		years.clear();
		years.addAll(h);
		return years;
	}

	public List<Integer> getMonthsByYear(int year) {
		List<Integer> months = new ArrayList<Integer>();
		List<SalaryRecord> list = (List<SalaryRecord>) salaryRecordDao.findAll();
		for(SalaryRecord sr:list) {
			if(sr.getAccountTime()!=null) {
				int y = GetYear.getYear(sr.getAccountTime());
				if(y == year) {
					months.add(GetMonth.getMonth(sr.getAccountTime()));
				}
			}
		}
		HashSet h = new HashSet(months);
		months.clear();
		months.addAll(h);
		return months;
	}

	public List<SalaryRecordDTO> findByYear(int year) {
		List<SalaryRecord> list = (List<SalaryRecord>) salaryRecordDao.findAll();
		List<SalaryRecordDTO> dtos = new ArrayList<SalaryRecordDTO>();
		for(SalaryRecord sr:list) {
			if(sr.getAccountTime()!=null) {
				int y = GetYear.getYear(sr.getAccountTime());
				if(y == year) {
					Staff staff = sr.getStaff();
					String staffName = null;
					String staffNum = null;
					float basicSalary = 0;
					if(sr.getBasicSalary()!=null)basicSalary = sr.getBasicSalary().getAmount();
					if(staff!=null) {
						staffName = staff.getStaffName();
						staffNum = staff.getStaffNum();
					}
					SalaryRecordDTO dto = new SalaryRecordDTO(sr,basicSalary,staffName,staffNum);
					System.out.println("按月查找到："+dto.getStaffName());
					dtos.add(dto);
				}
			}
		}
		return dtos;
	}

	public List<SalaryRecordDTO> findByYearAndMonth(int year,int month) {
		List<SalaryRecord> list = (List<SalaryRecord>) salaryRecordDao.findAll();
		List<SalaryRecordDTO> dtos = new ArrayList<SalaryRecordDTO>();
		for(SalaryRecord sr:list) {
			if(sr.getAccountTime()!=null) {
				int y = GetYear.getYear(sr.getAccountTime());
				int m = GetMonth.getMonth(sr.getAccountTime());
				if(y==year&&m==month) {
					Staff staff = sr.getStaff();
					String staffName = null;
					String staffNum = null;
					float basicSalary = 0;
					if(sr.getBasicSalary()!=null)basicSalary = sr.getBasicSalary().getAmount();
					if(staff!=null) {
						staffName = staff.getStaffName();
						staffNum = staff.getStaffNum();
					}
					SalaryRecordDTO dto = new SalaryRecordDTO(sr,basicSalary,staffName,staffNum);
					dtos.add(dto);
				}
			}
		}
		return dtos;
	}

	public Page<SalaryRecordDTO> findPage(String staffName, String staffNum, Integer year, Integer month,
			Pageable pageable) {
		List<SalaryRecordDTO> results = new ArrayList<SalaryRecordDTO>();
		List<SalaryRecord> list = new ArrayList<SalaryRecord>();
		if(staffName!=""&&staffName!=null) {
			System.out.println("sevice按员工姓名"+staffName);
			list = salaryRecordDao.findLikeStaffName(staffName);
		}else if(staffNum!=""&&staffName!=null) {
			System.out.println("sevice按员工编号"+staffNum);
			list = salaryRecordDao.findLikeStaffNum(staffNum);
		}else if(year!=null&&month!=null) {
			System.out.println("sevice按年和月");
			results = findByYearAndMonth(year, month);
		}else if(year!=null) {
			System.out.println("sevice按月");
			results = findByYear(year);
		}else {
			list = (List<SalaryRecord>) salaryRecordDao.findAll();
		}
		for(SalaryRecord sr:list) {
			Staff staff = sr.getStaff();
			String name = null;
			String num = null;
			float basicSalary = 0;
			if(sr.getBasicSalary()!=null)basicSalary = sr.getBasicSalary().getAmount();
			if(staff!=null) {
				name = staff.getStaffName();
				num = staff.getStaffNum();
			}
			results.add(new SalaryRecordDTO(sr,basicSalary,name,num));
		}
		List<SalaryRecordDTO> page = new ArrayList<SalaryRecordDTO>();
		int i = pageable.getPageNumber()*pageable.getPageSize();
		int n = i+pageable.getPageSize();
		while(i<results.size()&&i<n) {
			page.add(results.get(i));
			i++;
		}
		return new PageImpl<SalaryRecordDTO>(page,pageable,results.size());
	}

}
